package jmine.tec.aom.domain;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.Transient;

import jmine.tec.aom.si.PropertyTypeSystemIdentifier;
import jmine.tec.persist.annotation.Alias;
import jmine.tec.persist.annotation.UniqueConstraint;
import jmine.tec.persist.annotation.UniqueConstraints;

import org.hibernate.validator.NotNull;

import bancosys.tec.component.Documentation;
import bancosys.tec.persist.authorization.annotation.DisplayName;
import bancosys.tec.persist.bussobj.PersistableBusinessObject;
import bancosys.tec.persist.persister.annotation.NaturalKey;
import bancosys.tec.persist.persister.annotation.SystemId;
import bancosys.tec.utils.reflection.ReflectionUtils;

/**
 * Defines a property type and the corresponding property value class.
 * 
 * @author jrenaut
 */
@Entity
@Table(name = "TIPO_CARAC")
@Alias("TPCARC")
@SequenceGenerator(name = "SEQ_TPCARC", sequenceName = "SEQ_TPCARC")
@Documentation("ARMAZENA OS TIPOS DE CARACTERISTICAS DO SISTEMA (STRING, DOUBLE, ETC). TABELA PREENCHIDA VIA POST SCRIPT. NAO HA CADASTRO NO SISTEMA PARA INCLUSAO/ALTERACAO DOS SEUS DADOS.")
@DisplayName(value = "Tipo de Característica")
@UniqueConstraints({ @UniqueConstraint(name = "AK_TPCARC_0", columns = { "MNE_TIPO_CARAC" }) })
public class PropertyType extends PersistableBusinessObject {

    private Long id;

    private String mnemonic;

    private String propertyValueClass;

    /**
     * @return the id
     */
    @Id
    @SystemId
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "SEQ_TPCARC")
    @Column(name = "COD_TIPO_CARAC", nullable = false)
    @Documentation("CODIGO DO TIPO DE CARACTERISTICA.")
    public Long getId() {
        return this.id;
    }

    /**
     * @return the mnemonico
     */
    @NotNull
    @NaturalKey
    @Column(name = "MNE_TIPO_CARAC", nullable = false, updatable = false)
    @Documentation("MNEMONICO DO TIPO DE CARACTERISTICA.")
    @DisplayName(value = "Mnemônico", order = "0")
    public String getMnemonic() {
        return this.mnemonic;
    }

    /**
     * @return the propertyValueClass
     */
    @NotNull
    @Column(name = "CLASSE_CARAC_VALOR", nullable = false, updatable = false, length = 1024)
    @Documentation("NOME DA CLASSE QUE REPRESENTA VALORES DESTE TIPO DE CARACTERISTICA.")
    public String getPropertyValueClass() {
        return this.propertyValueClass;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param mnemonico the mnemonico to set
     */
    public void setMnemonic(String mnemonico) {
        this.mnemonic = mnemonico;
    }

    /**
     * @param propertyValueClass the propertyValueClass to set
     */
    public void setPropertyValueClass(String propertyValueClass) {
        this.propertyValueClass = propertyValueClass;
    }

    /**
     * Will throw an {@link IllegalStateException} if the given type cannot be found - this a database/configuration error.
     * 
     * @return the actual {@link AbstractPropertyValue} type to be used
     */
    @Transient
    @SuppressWarnings("unchecked")
    public Class<? extends AbstractPropertyValue> getPropertyValueType() {
        try {
            return (Class<? extends AbstractPropertyValue>) ReflectionUtils.findClass(this.getPropertyValueClass());
        } catch (ClassNotFoundException e) {
            throw new IllegalStateException("The property value type (" + this.getPropertyValueClass() + ") for property type "
                    + this.getMnemonic() + " was not found on the classpath.");
        }
    }

    /**
     * @param sys a {@link PropertyTypeSystemIdentifier}
     * @return true if this {@link PropertyType} is identified by the given {@link PropertyTypeSystemIdentifier}
     */
    @Transient
    public boolean isIdentifiedBy(PropertyTypeSystemIdentifier sys) {
        return this.getId().equals(sys.getSystemId());
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return this.getMnemonic();
    }

}
